﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using SF.DBUtility;
using SF.Common;//Please add references

namespace SF.DAL
{
	/// <summary>
	/// 数据访问类:sf_contract
	/// </summary>
	public partial class sf_contract
	{
        private string databaseprefix;  //数据库表名前缀
        public sf_contract(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
        }

		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select count(1) from " + databaseprefix + "contract");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt,8)};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SF.Model.sf_contract model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + databaseprefix + "contract(");
            strSql.Append("busnieseNum,contract_num,contract_name,agreement,document,firstpart,secondpart,first_represent,first_person,first_address,first_fax,second_represent,second_person,second_address,second_fax,create_date,create_people,update_date,is_delete)");
            strSql.Append(" values (");
            strSql.Append("@busnieseNum,@contract_num,@contract_name,@agreement,@document,@firstpart,@secondpart,@first_represent,@first_person,@first_address,@first_fax,@second_represent,@second_person,@second_address,@second_fax,@create_date,@create_people,@update_date,@is_delete)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@contract_num", SqlDbType.NVarChar,100),
					new SqlParameter("@contract_name", SqlDbType.NVarChar,100),
					new SqlParameter("@agreement", SqlDbType.Text),
					new SqlParameter("@document", SqlDbType.NVarChar,200),
					new SqlParameter("@firstpart", SqlDbType.NVarChar,100),
					new SqlParameter("@secondpart", SqlDbType.NVarChar,100),
					new SqlParameter("@first_represent", SqlDbType.NVarChar,100),
					new SqlParameter("@first_person", SqlDbType.NVarChar,100),
					new SqlParameter("@first_address", SqlDbType.NVarChar,200),
					new SqlParameter("@first_fax", SqlDbType.NVarChar,100),
					new SqlParameter("@second_represent", SqlDbType.NVarChar,100),
					new SqlParameter("@second_person", SqlDbType.NVarChar,100),
					new SqlParameter("@second_address", SqlDbType.NVarChar,200),
					new SqlParameter("@second_fax", SqlDbType.NVarChar,100),
					new SqlParameter("@create_date", SqlDbType.DateTime),
					new SqlParameter("@create_people", SqlDbType.NVarChar,100),
					new SqlParameter("@update_date", SqlDbType.DateTime),
					new SqlParameter("@is_delete", SqlDbType.Int,4)};
            parameters[0].Value = model.busnieseNum;
            parameters[1].Value = model.contract_num;
            parameters[2].Value = model.contract_name;
            parameters[3].Value = model.agreement;
            parameters[4].Value = model.document;
            parameters[5].Value = model.firstpart;
            parameters[6].Value = model.secondpart;
            parameters[7].Value = model.first_represent;
            parameters[8].Value = model.first_person;
            parameters[9].Value = model.first_address;
            parameters[10].Value = model.first_fax;
            parameters[11].Value = model.second_represent;
            parameters[12].Value = model.second_person;
            parameters[13].Value = model.second_address;
            parameters[14].Value = model.second_fax;
            parameters[15].Value = model.create_date;
            parameters[16].Value = model.create_people;
            parameters[17].Value = model.update_date;
            parameters[18].Value = model.is_delete;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(SF.Model.sf_contract model)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("update " + databaseprefix + "contract set ");
			strSql.Append("busnieseNum=@busnieseNum,");
			strSql.Append("contract_num=@contract_num,");
			strSql.Append("contract_name=@contract_name,");
			strSql.Append("agreement=@agreement,");
			strSql.Append("document=@document,");
			strSql.Append("firstpart=@firstpart,");
			strSql.Append("secondpart=@secondpart,");
			strSql.Append("first_represent=@first_represent,");
			strSql.Append("first_person=@first_person,");
			strSql.Append("first_address=@first_address,");
			strSql.Append("first_fax=@first_fax,");
			strSql.Append("second_represent=@second_represent,");
			strSql.Append("second_person=@second_person,");
			strSql.Append("second_address=@second_address,");
			strSql.Append("second_fax=@second_fax,");
			strSql.Append("create_date=@create_date,");
			strSql.Append("create_people=@create_people,");
			strSql.Append("update_date=@update_date,");
			strSql.Append("is_delete=@is_delete");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@contract_num", SqlDbType.NVarChar,100),
					new SqlParameter("@contract_name", SqlDbType.NVarChar,100),
					new SqlParameter("@agreement", SqlDbType.Text),
					new SqlParameter("@document", SqlDbType.NVarChar,200),
					new SqlParameter("@firstpart", SqlDbType.NVarChar,100),
					new SqlParameter("@secondpart", SqlDbType.NVarChar,100),
					new SqlParameter("@first_represent", SqlDbType.NVarChar,100),
					new SqlParameter("@first_person", SqlDbType.NVarChar,100),
					new SqlParameter("@first_address", SqlDbType.NVarChar,200),
					new SqlParameter("@first_fax", SqlDbType.NVarChar,100),
					new SqlParameter("@second_represent", SqlDbType.NVarChar,100),
					new SqlParameter("@second_person", SqlDbType.NVarChar,100),
					new SqlParameter("@second_address", SqlDbType.NVarChar,200),
					new SqlParameter("@second_fax", SqlDbType.NVarChar,100),
					new SqlParameter("@create_date", SqlDbType.DateTime),
					new SqlParameter("@create_people", SqlDbType.NVarChar,100),
					new SqlParameter("@update_date", SqlDbType.DateTime),
					new SqlParameter("@is_delete", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.BigInt,8)};
			parameters[0].Value = model.busnieseNum;
			parameters[1].Value = model.contract_num;
			parameters[2].Value = model.contract_name;
			parameters[3].Value = model.agreement;
			parameters[4].Value = model.document;
			parameters[5].Value = model.firstpart;
			parameters[6].Value = model.secondpart;
			parameters[7].Value = model.first_represent;
			parameters[8].Value = model.first_person;
			parameters[9].Value = model.first_address;
			parameters[10].Value = model.first_fax;
			parameters[11].Value = model.second_represent;
			parameters[12].Value = model.second_person;
			parameters[13].Value = model.second_address;
			parameters[14].Value = model.second_fax;
			parameters[15].Value = model.create_date;
			parameters[16].Value = model.create_people;
			parameters[17].Value = model.update_date;
			parameters[18].Value = model.is_delete;
			parameters[19].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool UpdateDel(int id)
		{
            string sql = "update " + databaseprefix + "contract set is_delete = 1 where id =" + id;
            
            int rows = DbHelperSQL.ExecuteSql(sql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("delete from " + databaseprefix + "contract ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SF.Model.sf_contract GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 id,busnieseNum,contract_num,contract_name,agreement,document,firstpart,secondpart,first_represent,first_person,first_address,first_fax,second_represent,second_person,second_address,second_fax,create_date,create_people,update_date,is_delete from " + databaseprefix + "contract ");
			strSql.Append(" where id=@id ");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt,8)};
			parameters[0].Value = id;

			SF.Model.sf_contract model=new SF.Model.sf_contract();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public SF.Model.sf_contract DataRowToModel(DataRow row)
		{
			SF.Model.sf_contract model=new SF.Model.sf_contract();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=long.Parse(row["id"].ToString());
				}
				if(row["busnieseNum"]!=null)
				{
					model.busnieseNum=row["busnieseNum"].ToString();
				}
				if(row["contract_num"]!=null)
				{
					model.contract_num=row["contract_num"].ToString();
				}
				if(row["contract_name"]!=null)
				{
					model.contract_name=row["contract_name"].ToString();
				}
				if(row["agreement"]!=null)
				{
					model.agreement=row["agreement"].ToString();
				}
				if(row["document"]!=null)
				{
					model.document=row["document"].ToString();
				}
				if(row["firstpart"]!=null)
				{
					model.firstpart=row["firstpart"].ToString();
				}
				if(row["secondpart"]!=null)
				{
					model.secondpart=row["secondpart"].ToString();
				}
				if(row["first_represent"]!=null)
				{
					model.first_represent=row["first_represent"].ToString();
				}
				if(row["first_person"]!=null)
				{
					model.first_person=row["first_person"].ToString();
				}
				if(row["first_address"]!=null)
				{
					model.first_address=row["first_address"].ToString();
				}
				if(row["first_fax"]!=null)
				{
					model.first_fax=row["first_fax"].ToString();
				}
				if(row["second_represent"]!=null)
				{
					model.second_represent=row["second_represent"].ToString();
				}
				if(row["second_person"]!=null)
				{
					model.second_person=row["second_person"].ToString();
				}
				if(row["second_address"]!=null)
				{
					model.second_address=row["second_address"].ToString();
				}
				if(row["second_fax"]!=null)
				{
					model.second_fax=row["second_fax"].ToString();
				}
				if(row["create_date"]!=null && row["create_date"].ToString()!="")
				{
					model.create_date=DateTime.Parse(row["create_date"].ToString());
				}
				if(row["create_people"]!=null)
				{
					model.create_people=row["create_people"].ToString();
				}
				if(row["update_date"]!=null && row["update_date"].ToString()!="")
				{
					model.update_date=DateTime.Parse(row["update_date"].ToString());
				}
				if(row["is_delete"]!=null && row["is_delete"].ToString()!="")
				{
					model.is_delete=int.Parse(row["is_delete"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,busnieseNum,contract_num,contract_name,agreement,document,firstpart,secondpart,first_represent,first_person,first_address,first_fax,second_represent,second_person,second_address,second_fax,create_date,create_people,update_date,is_delete ");
			strSql.Append(" FROM sf_contract ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,busnieseNum,contract_num,contract_name,agreement,document,firstpart,secondpart,first_represent,first_person,first_address,first_fax,second_represent,second_person,second_address,second_fax,create_date,create_people,update_date,is_delete ");
            strSql.Append(" FROM " + databaseprefix + "contract ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM sf_contract ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from sf_contract T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
		{
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from " + databaseprefix + "contract");
            if (strWhere.Trim() != "") 
            {
                strSql.Append(" where is_delete = 0 and" + strWhere );
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
		}

        /// <summary>
        /// 返回该商户合同记录总数
        /// 时间：2015-8-24
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public object GetCount(string str)
        {
            string sql = @"select count(*) from " + databaseprefix + "contract where busniesenum='{0}'";
            if (str.Trim() != "") 
            {
                sql = string.Format(sql, str);
            }
            return DbHelperSQL.GetSingle(sql.ToString());
        }

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

